package com.example.hr.text_android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.example.hr.text_android.fragment.fragment4;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private static final String TAG = "zuo";
    private TextView text1;
    private GestureDetector mGestureDetector;
    private Fragment fragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text1 = (TextView) findViewById(R.id.hello);
        text1.setOnClickListener(this);
        mGestureDetector = new GestureDetector(MainActivity.this, onGestureListener);
        fragment = new fragment4();
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
    }

    private GestureDetector.OnGestureListener onGestureListener =
            new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//                    return super.onFling(e1, e2, velocityX, velocityY);
                    // if (Math.abs(e1.getRawX() - e2.getRawX()) > 250) {
                    // // System.out.println("水平方向移动距离过大");
                    // return true;
                    // }
                    if (Math.abs(velocityY) < 100) {
                        // System.out.println("手指移动的太慢了");
                        Log.e(TAG, "手指移动的太慢了");
                        return true;
                    }

                    // 手势向下 down
                    if ((e2.getRawY() - e1.getRawY()) > 200) {
//                        finish();//在此处控制关闭
                        Log.e(TAG, "手势向下");
                        return true;
                    }
                    // 手势向上 up
                    if ((e1.getRawY() - e2.getRawY()) > 200) {
                        Log.e(TAG, "手势向上");
                        text1();
                        return true;
                    }
                    return super.onFling(e1, e2, velocityX, velocityY);
                }
            };

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        return super.onTouchEvent(event);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.hello:
                Log.d(TAG, "onClick:hello ");
//                fragmentTransaction.add(R.id.fragment_container,fragment);
//                fragmentTransaction.commit();
//                Intent intent = new Intent(this, Main3Activity.class);
//                startActivity(intent);

                break;
        }
    }

    private void text1() {
//        Intent intent = new Intent(this, Main2Activity.class);
        Intent intent = new Intent();
        intent.setClassName("com.example.hr.text_android", "com.example.hr.text_android.Main2Activity");
        startActivity(intent);
//        overridePendingTransition(R.anim.activity_open,R.anim.activity_close);
    }
}
